/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.bangong.wjjbxxl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.bangong.wjjbxxl.domain.Wjjbxx;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.bangong.wjjbxxl.service.WjjbxxService;
import me.zhengjie.modules.bangong.wjjbxxl.domain.dto.WjjbxxQueryCriteria;
import me.zhengjie.modules.bangong.wjjbxxl.mapper.WjjbxxMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author Houger
* @date 2025-03-09
**/
@Service
@RequiredArgsConstructor
public class WjjbxxServiceImpl extends ServiceImpl<WjjbxxMapper, Wjjbxx> implements WjjbxxService {

    private final WjjbxxMapper wjjbxxMapper;

    @Override
    public PageResult<Wjjbxx> queryAll(WjjbxxQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(wjjbxxMapper.findAll(criteria, page));
    }

    @Override
    public List<Wjjbxx> queryAll(WjjbxxQueryCriteria criteria){
        return wjjbxxMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Wjjbxx resources) {
        wjjbxxMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Wjjbxx resources) {
        Wjjbxx wjjbxx = getById(resources.getColumnId());
        wjjbxx.copy(resources);
        wjjbxxMapper.update(wjjbxx,new QueryWrapper<>());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        wjjbxxMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<Wjjbxx> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Wjjbxx wjjbxx : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("文件编号", wjjbxx.getWjbh());
            map.put("文件文号", wjjbxx.getWjwh());
            map.put("标题", wjjbxx.getBt());
            map.put("主题词", wjjbxx.getZtc());
            map.put("密级码", wjjbxx.getMjm());
            map.put("紧急程度码", wjjbxx.getJjcdm());
            map.put("页数", wjjbxx.getYs());
            map.put("文件分类码", wjjbxx.getWjflm());
            map.put("正文", wjjbxx.getZw());
            map.put("附件", wjjbxx.getFj());
            map.put("发文日期", wjjbxx.getFwrq());
            map.put("抄送单位", wjjbxx.getCsdw());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}